<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Statistics Library
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          xiazhang
 * @license         MIT
 */
class Statistics {
    protected $ci;
    private $db;
    public function __construct() {
        $this->ci = & get_instance();
        $this->db = $this->ci->load->database('admin',true);
    }

	public function getDst($date){
		$where	=	array('date'=>$date);
        $this->db->select()->from("cfd_report_dst")->where($where);
		
        $query =  $this->db->get();
        return $query->row_array();
    }

	public function getTrade($where,$sdate="",$edate=''){
		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}

		if(!empty($where)){
			$this->db->where($where);
		}

        $query = $this->db->select()->from("cfd_report_user_trade")->get();
		
        return $query->result_array();
    }

	public function getTradeTotalByUid($uid){
		$where	=	array('uid'=>$uid);
        $this->db->select()->from("cfd_report_user_tradeTotal")->where($where);
		
        $query =  $this->db->get();
        return $query->row_array();
    }

	public function getTradeByUid($uid,$sdate,$edate){
		$where	=	array('uid'=>$uid);

		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}

        $this->db->select()->from("cfd_report_user_trade")->where($where)->order_by('date DESC');
		
        $query =  $this->db->get();
        return $query->result_array();
    }

	public function getTradeSumByUid($uid,$sdate='',$edate=''){
		$where	=	array('uid'=>$uid);

		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}

        $this->db->select("sum(cny) as cny,sum(ticket_cny) as ticket_cny,sum(num) as num,sum(ticket_num) as ticket_num,sum(fee) as fee,sum(yk) as yk,sum(ticket_yk) as ticket_yk")->from("cfd_report_user_trade")->where($where);
		
        $query =  $this->db->get();
        return $query->row_array();
    }

	public function getTradeTotalLastTime(){
        $this->db->select()->from("cfd_report_user_tradeTotal")->order_by('datetime DESC')->limit(1);
		
        $query =	$this->db->get();
		$row   =	$query->row_array();
        return $row['datetime'];
    }
}

?>